package com.yy.xshell;

import com.alibaba.fastjson2.JSONObject;
import com.yy.xshell.auth.UserSessionInfo;
import com.yy.xshell.core.ShellContext;
import com.yy.xshell.core.SshCore;
import com.yy.xshell.exceptions.AuthFailException;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

/**
 * @author wxy
 * @version 1.0
 * @date 2024/8/7 17:47
 */
public enum EventType {



    OPEN_SESSION(1, "新建会话"){
        public void invoke(SshMsgDtO sshMsgDtO) {
            UserSessionInfo sessionInfo = JSONObject.parseObject(sshMsgDtO.getContent(), UserSessionInfo.class);
            sessionInfo.setUserId(sshMsgDtO.getUserId());
            ShellContext shellContext = new ShellContext(sshCore.open(sessionInfo), sessionInfo.getSessionKey(), sshMsgDtO.getEnvironment());
            try {
                sshCore.setEnvironment(sshMsgDtO);
                sshMsgDtO.getChannel().writeAndFlush(new TextWebSocketFrame(SshCore.RESULT_HANDLER_PIPELINE.handler(shellContext).toJsonString()));
            }catch (AuthFailException e){
                shellContext.setMsg(String.format("<div>%s</div>", e.getMessage()));
                sshMsgDtO.getChannel().writeAndFlush(new TextWebSocketFrame(shellContext.toJsonString()));
            }

        }
    },

    SEND_SHELL(2,"发送shell命令"){
        public void invoke(SshMsgDtO sshMsgDtO) {
            JSONObject params = JSONObject.parseObject(sshMsgDtO.getContent());
            String sessionKey = sshMsgDtO.getSessionKey();
            String userId = sshMsgDtO.getUserId();
            SshCore.getUserSessionInfo(userId, sessionKey);
            sshCore.setEnvironment(sshMsgDtO);
            sshCore.sendShell(sshMsgDtO.getUserId(), sessionKey, params.getString("command"), sshMsgDtO.getChannel());
        }
    },

    SEND_COMMAND(3, "发送exec命令"){
        public void invoke(SshMsgDtO sshMsgDtO) {
            sshCore.setEnvironment(sshMsgDtO);
            JSONObject jsonObject = JSONObject.parseObject(sshMsgDtO.getContent());
            sshCore.sendCommand(sshMsgDtO.getUserId(), sshMsgDtO.getSessionKey(), jsonObject.getString("command"), sshMsgDtO.getChannel());
        }
    },

    COMMENT_CLOSE(4, "结束正在执行的命令"){
        public void invoke(SshMsgDtO sshMsgDtO) {
            JSONObject jsonObject = JSONObject.parseObject(sshMsgDtO.getContent());
            sshCore.commentClose(sshMsgDtO.getUserId(), sshMsgDtO.getSessionKey());

        }
    },

    BREAK_CHANNEL(5,"结束会话"){
        public void invoke(SshMsgDtO sshMsgDtO) {

        }
    },

    BREAK_ALL_CHANNEL(6,"结束用户全部会话"){
        public void invoke(SshMsgDtO sshMsgDtO) {
            sshCore.closeByUserId(sshMsgDtO.getUserId());
        }
    }
    ;

    private final Integer type;

    private final String remark;

    protected final SshCore sshCore = SshCore.instanceOf();

    EventType(Integer type, String remark){
        this.type = type;
        this.remark = remark;
    }

    public static EventType getByType(int type){
        EventType[] values = EventType.values();
        for(EventType eventType : values){
            if(eventType.type.equals(type)){
                return eventType;
            }
        }
        return null;
    }

    public abstract void invoke(SshMsgDtO sshMsgDtO);


}
